//
//  MHNavibarView.m
//  MHO
//
//  Created by papo on 2/24/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHNavibarView.h"
#import "MHAppDelegate.h"
#import "MHDownloadViewController.h"
#import "MHSearchResultViewController.h"


@implementation MHNavibarView


+ (MHNavibarView*)create {
    return [self createWithLeftButton:Nil rightButton:nil searchButton:YES];
}

+ (MHNavibarView*)createWithLeftButton:(NSString *)leftBt rightButton:(NSString *)rightBt searchButton:(BOOL)show {
    return [self createWithTitle:nil leftButton:leftBt rightButton:rightBt searchButton:show];
}

+ (MHNavibarView*)createWithTitle:(NSString *)title leftButton:(NSString *)leftBt rightButton:(NSString *)rightBt searchButton:(BOOL)show {
    MHNavibarView* navi = [[[NSBundle mainBundle] loadNibNamed:@"NavibarView" owner:self options:nil] objectAtIndex:0];
    [navi setTitle:title leftButton:leftBt rightButton:rightBt searchButton:show];
    return navi;
//    return [[self alloc] initWithTitle:title leftButton:leftBt rightButton:rightBt searchButton:show];
}

- (id)initWithTitle:(NSString *)title leftButton:(NSString *)leftBt rightButton:(NSString *)rightBt searchButton:(BOOL)show {
    if (self = [super init]) {
        [self setTitle:title leftButton:leftBt rightButton:rightBt searchButton:show];
        self.rightButton.exclusiveTouch = YES;
        self.leftButton.exclusiveTouch = YES;
    }
    return self;
}

- (void)setTitle:(NSString *)title leftButton:(NSString *)leftBt rightButton:(NSString *)rightBt searchButton:(BOOL)show {
    if (title) {
        self.title.text = title;
    }
    
    if (leftBt) {
        [self.leftButton setImage:[UIImage imageNamed:leftBt] forState:UIControlStateNormal];
    }
    
    if (rightBt) {
        [self.rightButton setImage:[UIImage imageNamed:rightBt] forState:UIControlStateNormal];
    }
    
    self.searchButton.hidden = !show;
}

- (void)setActionSelectorDelegate:(id)target left:(SEL)left right:(SEL)right search:(SEL)search {
    if (target) {
        if (left) {
            [self.leftButton addTarget:target action:left forControlEvents:UIControlEventTouchUpInside];
        }
        
        if (right) {
            [self.rightButton addTarget:target action:right forControlEvents:UIControlEventTouchUpInside];
        }
        
        if (search) {
            [self.searchButton addTarget:target action:search forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (IBAction)leftButtonPressed:(id)sender {
    NSLog(@"override this method!");
}

- (IBAction)rightButtonPressed:(id)sender {
    NSLog(@"override this method! call default action! show download viewcontroller!");
    if (self.rootViewController) {
        MHDownloadViewController* vc = [[MHDownloadViewController alloc] initWithNibName:@"MHDownloadViewController" bundle:nil];
        [self.rootViewController.navigationController pushViewController:vc animated:YES];
    }
}

- (IBAction)searchButtonPressed:(id)sender {
    if (self.rootViewController) {
        MHSearchResultViewController* vc = [[MHSearchResultViewController alloc] initWithNibName:@"MHSearchResultViewController" bundle:nil];
        [self.rootViewController.navigationController pushViewController:vc animated:YES];
    }
}

@end
